import {createSlice} from '@reduxjs/toolkit';
import {ProjectState} from '@/schemas/project'; // 确保路径正确
import {CreateProject, ImportProject, ListProject, LoadProject} from "@/service/project.ts";

// 定义 user state 的初始值
const initialState: ProjectState = {
    list: []
};

const projectSlice = createSlice({
    name: 'project',
    initialState,
    reducers: {},
    extraReducers: (builder) => {
        builder.addCase(ListProject.fulfilled, (state, action) => {
            state.list = action.payload;
        })
        builder.addCase(CreateProject.fulfilled, (state, action) => {
            state.list = action.payload
            if (action.payload && action.payload.length) state.selected = action.payload[0];
            else state.selected = state.list[0]
        })
        builder.addCase(ImportProject.fulfilled, (state, action) => {
            state.list = action.payload
            if (action.payload && action.payload.length) state.selected = action.payload[0];
            else state.selected = state.list[0]
        })
        builder.addCase(LoadProject.fulfilled, (state, action) => {
            state.selected = action.payload;
        })
    }
});

// 导出 actions
export const PROJECT = projectSlice.actions;

// 导出 reducer
export default projectSlice.reducer;